<!-- 排班管理 -->
<template>
  <div class="dashboard-container transport-task customer-list-box">
    <div class="app-container">
      <!-- 搜索 -->
      <el-card
        shadow="never"
        class="search-card-box"
        style="margin-bottom: 20px;"
      >
        <el-form
          ref="workManageSearchFormData"
          :model="workManageSearchFormData"
          label-width="90px"
        >
          <el-row :gutter="60">
            <el-col :span="8">
              <el-form-item
                label="员工账号:"
                style="margin-bottom: 0px"
              >
                <el-input
                  v-model="workManageSearchFormData.employeeNumber"
                  placeholder="请输入员工账号"
                  clearable
                />
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item
                label="员工姓名:"
                style="margin-bottom: 0px"
              >
                <el-input
                  v-model="workManageSearchFormData.name"
                  placeholder="请输入员工姓名"
                  clearable
                />
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item
                label="工作模式:"
                style="margin-bottom: 20px"
              >
                <el-select
                  v-model="workManageSearchFormData.workPatternId"
                  placeholder="请选择工作模式"
                  clearable
                >
                  <el-option
                    v-for="item in workMode"
                    :key="item.id"
                    :label="item.name"
                    :value="item.id"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item
                label="月份:"
                style="margin-bottom: 0px"
              >
                <el-date-picker
                  v-model="workManageSearchFormData.month"
                  type="month"
                  placeholder="选择月"
                  value-format="yyyy-MM"
                >
                </el-date-picker>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item
                prop="agencyId"
                label="所属机构:"
                style="margin-bottom: 0px"
              >
                <treeselect
                  v-model="workManageSearchFormData.agencyId"
                  :options="agencyOptions"
                  :normalizer="normalizer"
                  :searchable="true"
                  style="width: 100%"
                  no-options-text="暂无数据"
                  no-results-text="暂无数据"
                  placeholder="请选择所属机构"
                  @select="selectEndAgency"
                  @open="opens"
                  @close="closes"
                />
                <img
                  ref="arrows"
                  src="../../assets/pullDown.png"
                  style="
                    position: absolute;
                    width: 18px;
                    height: 18px;
                    right: 9px;
                    top: 12px;
                    cursor: pointer;
                    pointer-events: none;
                    transition-duration: 0.3s;
                  "
                />
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-button
                type="warning"
                @click="searchFn"
              >搜索</el-button>
              <el-button
                plain
                class="reset-btn"
                @click="resetForm('workManageSearchFormData')"
              >重置</el-button>
            </el-col>
          </el-row>
        </el-form>
      </el-card>
      <!-- 列表 -->
      <el-card
        v-loading="loading"
        class="box-card"
        shadow="never"
        element-loading-text="加载中"
        style="margin-top: 20px;"
      >
        <el-button
          type="warning"
          style="margin-bottom:20px; margin-right: 20px;"
          @click="$router.push('workArrange-setting')"
        >排班设置</el-button>
        <el-button
          type="warning"
          style="margin-bottom:20px"
          class="cancel-btn"
          @click="bindScheduling"
        >绑定排班</el-button>
        <!-- 列表 -->
        <el-table
          :data="tableData"
          stripe
          style="width: 100%;"
          @selection-change="handleSelectionChange"
        >
          <el-table-column
            type="selection"
            width="60"
            fixed
          >
          </el-table-column>
          <el-table-column
            prop="employeeNumber"
            label="员工账号"
            fixed
            width="100"
          ></el-table-column>
          <el-table-column
            prop="name"
            label="员工姓名"
            fixed
            width="120"
          ></el-table-column>
          <el-table-column
            prop="phone"
            label="员工电话"
            fixed
            width="120"
          ></el-table-column>
          <el-table-column
            prop="userTypeName"
            label="员工角色"
            fixed
          ></el-table-column>
          <el-table-column
            prop="workPatternName"
            label="当前工作模式"
            fixed
            width="110"
          ></el-table-column>
          <template v-if="searchFlag">
            <el-table-column
              v-for="(item, index) in tableData[0].workSchedules.length"
              :key="index"
              :label="(index + 1).toString()"
            >
              <template #default="scope">
                <div :class="{'rest': scope.row.workSchedules[item - 1] === false, 'work': scope.row.workSchedules[item - 1] === true}">{{ scope.row.workSchedules[item - 1] ? '上' : '休' }}</div>
              </template>
            </el-table-column>
          </template>
          <!-- 搜索不到内容时显示 -->
          <el-card
            v-show="
              (!tableData || tableData.length <= 0 && !loading && searchkey)
            "
            slot="empty"
            shadow="never"
          >
            <empty style="text-align: center"></empty>
          </el-card>
        </el-table>
        <!-- 分页 -->
        <div
          v-show="tableData && tableData.length > 0"
          class="pagination"
        >
          <div class="pages">
            <el-pagination
              :current-page="Number(workManageSearchFormData.page)"
              :total="Number(total)"
              :page-size="Number(workManageSearchFormData.pageSize)"
              :page-sizes="[10, 20, 30, 50]"
              layout="total, sizes, prev, pager, next, jumper"
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
            />
          </div>
        </div>
      </el-card>
      <!-- 人工调整弹窗 -->
      <manualAdjustment
        ref="manualAdjustment"
        :work-mode-list="artificialWorkModelList"
        :form-base="formData"
        @newDataes="getListDia"
      ></manualAdjustment>
    </div>
  </div>
</template>
<script>
import '@riophae/vue-treeselect/dist/vue-treeselect.css'
import Treeselect from '@riophae/vue-treeselect'
import { agencyList } from '@/api/institutions'
import {
  selectWorkHistoryList,
  workManage
} from '@/api/transit'
import manualAdjustment from './components/manual-adjustment.vue'
export default {
  components: {
    Treeselect,
    manualAdjustment
  },
  data() {
    return {
      // 对树状结构进行解析返回数据
      normalizer(node) {
        return {
          id: node.id,
          label: node.name,
          children: node.children
        }
      },
      agencyOptions: [],
      // 搜索表单
      workManageSearchFormData: {
        page: 1,
        pageSize: 10,
        agencyId: null,
        employeeNumber: '',
        month: '',
        name: '',
        userType: '',
        workPatternId: null
      },
      workMode: [],
      loading: false,
      // 列表数据集合
      tableData: [],
      // 员工角色数据转换
      userTypeObj: {
        1: '员工',
        2: '快递员',
        3: '司机'
      },
      searchFlag: false,
      searchkey: false,
      // 总数量
      total: 0,
      //  选中的数据
      tableSelection: [],
      // 子组件的工作模式列表
      artificialWorkModelList: [],
      formData: {
        workPatternType: '1',
        workPatternId: '',
        userIdList: []
      }
    }
  },
  created() {
    this.getAgencyList()
    this.getAllWorkModeList()
    this.getWorkManageList()
  },
  methods: {
    opens() {
      this.$refs.arrows.style.transform = 'rotate(-180deg)'
    },
    closes() {
      this.$refs.arrows.style.transform = 'rotate(0deg)'
    },
    selectEndAgency(val) {
      this.workManageSearchFormData.agencyId = val.id
      // console.log(val)
    },
    // 获取所属机构列表数据
    async getAgencyList() {
      const { data: res } = await agencyList()
      // 获取数据给options
      this.agencyOptions = JSON.parse(res)
      // console.log(this.agencyOptions)
    },
    // 获取所有工作模式数据
    async getAllWorkModeList() {
      const { data: res } = await selectWorkHistoryList()
      // console.log(res)
      this.workMode = res[1].concat(res[2])
      this.workMode.push({
        name: '暂无排班',
        id: ''
      })
      this.artificialWorkModelList = res[1]
      // console.log(this.workMode)
    },
    clearField() {
      // 如果其他搜索项没有值则不传给后端
      if (this.workManageSearchFormData.employeeNumber === '') {
        this.$delete(this.workManageSearchFormData, 'employeeNumber')
      }
      if (this.workManageSearchFormData.month === '') {
        this.$delete(this.workManageSearchFormData, 'month')
      }
      if (this.workManageSearchFormData.name === '') {
        this.$delete(this.workManageSearchFormData, 'name')
      }
      if (this.workManageSearchFormData.userType === '') {
        this.$delete(this.workManageSearchFormData, 'userType')
      }
      if (this.workManageSearchFormData.workPatternId === '') {
        this.$delete(this.workManageSearchFormData, 'workPatternId')
      }
    },
    setField() {
      // 对搜索项属性设置响应式，便于重置方法生效
      this.$set(this.workManageSearchFormData, 'employeeNumber')
      this.$set(this.workManageSearchFormData, 'month')
      this.$set(this.workManageSearchFormData, 'name')
      this.$set(this.workManageSearchFormData, 'userType')
      this.$set(this.workManageSearchFormData, 'workPatternId')
    },
    // 搜索功能
    async searchFn() {
      this.clearField()
      this.workManageSearchFormData.page = 1
      const flag = await this.getWorkManageList()
      this.searchkey = true
      if (
        this.workManageSearchFormData.month === '' &&
        this.workManageSearchFormData.name === '' &&
        this.workManageSearchFormData.agencyId === null &&
        this.workManageSearchFormData.employeeNumber === '' &&
        this.workManageSearchFormData.userType === '' &&
        this.workManageSearchFormData.workPatternId === null
      ) {
        this.searchFlag = false
      } else if (flag === false) {
        this.searchFlag = false
      } else {
        this.searchFlag = true
      }
    },
    // 重置搜索项
    resetForm(workManageSearchFormData) {
      this.setField()
      this.searchkey = false
      this.searchFlag = false
      this.$refs[workManageSearchFormData].resetFields()
      this.getWorkManageList()
    },
    // 每页显示信息条数
    handleSizeChange(val) {
      this.workManageSearchFormData.page === 1
      this.workManageSearchFormData.pageSize = val
      this.getWorkManageList()
    },
    // 进入某一页
    handleCurrentChange(val) {
      this.workManageSearchFormData.page = val
      this.getWorkManageList()
    },
    // 获取排班管理列表
    async getWorkManageList() {
      this.loading = true
      const res = await workManage(this.workManageSearchFormData)
      console.log(res)
      if (res.code !== 200) {
        this.loading = false
        this.searchkey = true
        this.tableData = []
        return false
      }
      if (res.code === 200) {
        this.tableData = res.data.items
        this.tableSelection = []
        this.total = res.data.counts
        this.tableData.forEach(item => {
          item.userTypeName = this.userTypeObj[item.userType]
        })
        this.loading = false
        if (res.data.items.length === 0) {
          this.loading = false
          return false
        }
      }
      // console.log('排班日期::', this.tableData[0].workSchedules)
    },
    // 列表选择
    handleSelectionChange(val) {
      this.tableSelection = val
    },
    // 绑定排班
    bindScheduling() {
      if (this.tableSelection.length <= 0) {
        this.$message({
          type: 'error',
          message: '请选择调整人员'
        })
        return
      }
      this.tableSelection.forEach(item => {
        this.formData.userIdList.push(item.id)
      })
      this.$refs.manualAdjustment.dialogFormV()
    },
    // 修改排班成功，更新列表
    getListDia() {
      this.getWorkManageList()
      this.formData = {
        workPatternType: '1',
        workPatternId: '',
        userIdList: []
      }
    }
  }
}
</script>
<style rel="stylesheet/scss" lang="scss" scoped>
::v-deep th.date{
  .cell{
    padding-left: 17px!important;
  }
}
.work {
  width: 24px;
  height: 24px;
  background: #ffeeeb;
  border: 1px solid #e15536;
  border-radius: 50%;
  text-align: center;
  color: #e15536;
  font-size: 13px;
}
.history-name {
  width: auto !important;
  ::v-deep .el-input__inner {
    width: 293px;
  }
}

.workArrange-manage-people-setting {
  ::v-deep .el-dialog__body {
    padding: 30px 50px 10px 50px !important;
  }
  ::v-deep .el-dialog__footer {
    padding-bottom: 40px;
  }
}

.work-type {
  ::v-deep .el-form-item__content {
    margin-left: 40px !important;
  }
  ::v-deep .el-radio:first-child {
    margin-right: 80px !important;
  }
  ::v-deep.el-radio__label {
    font-weight: bold;
    color: #333333;
  }
}

.dialog-footer {
  .el-button {
    width: 89px;
    height: 36px;
    padding: 10px 20px;
  }
}

.rest {
  width: 24px;
  height: 24px;
  background: #eff3f8;
  border: 1px solid #d8dde3;
  border-radius: 50%;
  text-align: center;
  color: #20232a;
  font-size: 13px;
}

.alert {
  margin: 10px 0px;
}
.pagination {
  margin-top: 20px;
  padding-bottom: 40px;
  display: flex;
  justify-content: center;
}
.transport-task ::v-deep .el-table td,
.el-table th {
  padding: 12px 0;
  min-width: 0;
  -webkit-box-sizing: border-box;
  box-sizing: border-box;
  text-overflow: ellipsis;
  vertical-align: middle;
  position: relative;
  text-align: left;
  overflow: hidden;
}

// 下拉框
::v-deep .el-select {
  width: 100%;
}

/*定义滚动条轨道 内阴影+圆角*/
::v-deep .el-table--scrollable-x .el-table__body-wrapper {
  padding-bottom: 5px;
  margin-bottom: 5px;
  &::-webkit-scrollbar {
    height: 10px;
  }

  /*定义滑块 内阴影+圆角*/
  &::-webkit-scrollbar-thumb {
    border-radius: 10px;
    background-color: rgba(144, 147, 153, 0.3);
  }

  &::-webkit-scrollbar-track-piece {
    margin-right: 3px;
    margin-left: 3px;
  }
}
::v-deep .el-card{
  overflow: initial;
  .cancel-btn {
    width: 110px;
    height: 40px;
    color: #e15536;
    border: 1px solid #e15536;
    background-color: #ffeeeb;
  }
  .cancel-btn:hover {
    background-color: #e15536;
    color: #fff;
  }
  .rest {
    width: 24px;
    height: 24px;
    background-color: #eff3f8;
    border: 1px solid #d8dde3;
    border-radius: 50%;
    text-align: center;
    color: #20232a;
    font-size: 13px;
  }
  .work {
    width: 24px;
    height: 24px;
    background-color: #ffeeeb;
    border: 1px solid #e15536;
    border-radius: 50%;
    text-align: center;
    color: #e15536;
    font-size: 13px;
  }
}
</style>

<style></style>
